package com.carrot.mq.mqserver.datacenter;

import com.carrot.mq.MqApplication;
import com.carrot.mq.mqserver.core.Binding;
import com.carrot.mq.mqserver.core.Exchange;
import com.carrot.mq.mqserver.core.ExchangeType;
import com.carrot.mq.mqserver.core.MSGQueue;
import com.carrot.mq.mqserver.mapper.MetaMapper;

import java.io.File;
import java.util.List;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/8/1 17:32
 * 数据库操作的整合
 */
public class DataBaseManager {

    private MetaMapper metaMapper;

    /**
     * 初始化: 建库、建表、插入默认数据
     */
    public void init() {
        // 手动获取 MetaMapper
        metaMapper = MqApplication.context.getBean(MetaMapper.class);
        if (!checkDBExists()) {
            // 数据库不存在, 建表相关操作
            // 创建 data 目录
            File dataDir = new File("./data");
            dataDir.mkdirs();
            // 创建数据库表
            createTable();
            // 插入默认数据
            createDefaultData();
            System.out.println("[DataBaseManager] 数据库初始化完成!");
        } else {
            System.out.println("[DataBaseManager] 数据库已经存在。");
        }
    }

    /**
     * 删除 meta.db
     */
    public void deleteDB() {
        // 删除 meta.db
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if (ret) {
            System.out.println("[DataBaseManager] 删除数据库文件成功!");
        } else {
            System.out.println("[DataBaseManager] 删除数据库文件失败!");
        }
        // 删除目录
        File dataDir = new File("./data");
        ret = dataDir.delete();
        if (ret) {
            System.out.println("[DataBaseManager] 删除数据库目录成功!");
        } else {
            System.out.println("[DataBaseManager] 删除数据库目录失败!");
        }
    }

    /**
     * 判断 meta.db 是否存在
     */
    private boolean checkDBExists() {
        File file = new File("./data/meta.db");
        if (file.exists()) {
            return true;
        }
        return false;
    }

    /**
     * 建表
     */
    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager] 创建表完成!");
    }

    /**
     * 添加默认数据, 主要是添加一个默认的交换机
     */
    private void createDefaultData() {
        // 构造默认的交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 创建初始数据完成!");
    }

    /**
     * 插入、查询与删除相关数据库操作
     */
    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }

    public List<Exchange> selectAllExchanges() {
        return metaMapper.selectAllExchanges();
    }

    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MSGQueue msgQueue) {
        metaMapper.insertQueue(msgQueue);
    }

    public List<MSGQueue> selectAllQueues() {
        return metaMapper.selectAllQueues();
    }

    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }

    public List<Binding> selectAllBindings() {
        return metaMapper.selectAllBindings();
    }

    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }
}
